package gbench.appdemo.myworld.junit;

import static gbench.appdemo.myworld.market.commodity.Commodity.*;
import static gbench.common.tree.LittleTree.IRecord.REC;
import gbench.common.tree.LittleTree.IRecord;

import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.*;
import org.junit.jupiter.api.Test;

import gbench.appdemo.myworld.economy.Economy;
import gbench.appdemo.myworld.market.SimpleMarket;
import gbench.appdemo.myworld.market.SimpleMarket.TradeTransaction;
import gbench.appdemo.myworld.market.commodity.Commodity;
import gbench.appdemo.myworld.market.license.CommodityLicenseGroup;
import gbench.appdemo.myworld.util.Latch;
import gbench.common.fs.FileSystem;
import gbench.common.fs.XlsFile.SimpleExcel;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 
 * @author gbench
 *
 */
public class JunitDisruptor {
    
    @Data @AllArgsConstructor
    public class Resource{
        private String name;
        private String unit;
        private Double amount;
        private String type;
    }
    
    @Data @AllArgsConstructor
    public class Area{
        private String name;
        private Map<String,Resource> resources;
    }
    
    /**
     * 资源读取
     */
    @Test
    public void bar() {
        var f = new SimpleExcel();
        final var path = "C:\\Users\\gbench\\Desktop\\地理.xlsx";
        f.loadXlsx(FileSystem.stream(path));
        var xx = f.autoDetect("自然资源");
        xx.mapByRow(IRecord::REC).forEach(rec->{
            System.out.println(rec);
        });
        f.close();
    }

    @Test
    public void foo() throws InterruptedException {
        final var latch = new Latch(1);
        final var program = new Latch(1);
        final var size = (int) Math.pow(2, 10);

        // 经济生产
        new Economy(size)
        .produce(eco -> {// 工业生产
            Stream.iterate(0, k -> k + 1).limit(size).parallel().forEach(k -> {
                eco.publish(REC("i", k));
            });
            latch.countdown();
        }).consume(event -> {// 社会生活
            System.out.println(event);
        }).collect(e -> {
            latch.await();
            System.out.println("shutdown");
            e.shutdown();
            program.countdown();
        });// collect
        
        program.await();
        System.out.println("经济体结束");
    }
    
    @Test
    public void qux() {
        final var rmb = CMDT("RMB");
        final var labor = COMMODITY("RMB","人日");
        final var market = SimpleMarket.of("商品市场",rmb);// 创建一个市场经济
        
        final var mobile= market.getCommodityLicense("中国移动");
        final var huawei= market.getCommodityLicense("华为");
        final var zhangsan= market.getCommodityLicense("张三",labor);
        
        // 市场主体
        mobile.purchase(huawei, CMDT("通讯设置"), 10000000);
        zhangsan.purchase(mobile, CMDT("通话服务"), 100);
        mobile.purchase(zhangsan, CMDT("劳务"), 8000);
        
        market.getTransactions().stream().flatMap(deal->Stream.of(
            REC("trader",deal.getPartA(),"deal",deal,"drcr",0),// 借方
            REC("trader",deal.getPartB(),"deal",deal,"drcr",1) // 贷方
        )).collect(Collectors.groupingBy(e->e.str("trader"))).forEach((k,dd)->{
            final var total = dd.stream().map(e->{
                final var d = e.get("deal",TradeTransaction.class);
                return (e.i4("drcr")==0?-1:1)*d.getPrice().getAAmount();
            }).collect(Collectors.summarizingDouble(e->e));
            System.out.println(REC("name",k,"total",total.getSum()));
        });
    }
    
    /**
     * 
     */
    @Test
    public void qux2() {
        final var market = SimpleMarket.of("自然市场",COMMODITY("稻米","公斤"));// 创建一个市场经济
        
        final var farmland = market.getCommodityLicense("稻田");
        final var zhangsan = market.getCommodityLicense("张三",COMMODITY("工作日","天"));// 张三劳动100天霍地500公斤稻米
        zhangsan.work(farmland, 100, 500);
        market.getTransactions().forEach(e->{
            System.out.println(e);
        });
        
        final var licenseGroup = new CommodityLicenseGroup("张三",zhangsan,farmland);
        licenseGroup.getTransaction().forEach(e->{
            System.out.println(e);
        });
        
        // 资本市场
        final var rmb = COMMODITY("货币","RMB");
        final var capitalMarket = new SimpleMarket<Commodity>("自然市场",rmb);// 创建一个市场经济
        final var beidahuang = capitalMarket.getCommodityLicense("吉林省土地管理局",COMMODITY("100亩土地使用权","30年"));
        final var zhoubapi = capitalMarket.getCommodityLicense("周扒皮",rmb);
        zhoubapi.purchase(beidahuang, 5000000, 30);// 使用5百万 构面30年土地使用权
        capitalMarket.getTransactions().forEach(e->{
            System.out.println(e);
        });
        
        
    }
    
    /**
     * 
     */
    @Test
    public void qux3() {
        final var grain = COMMODITY("粮食","公斤");
        final var wheal = COMMODITY("麦子","公斤");
        final var rmb  = COMMODITY("RMB","元");
        final var labor  = COMMODITY("产业劳动","天");
        final var tractor = COMMODITY("拖拉机","台");
        final var noodel = COMMODITY("拉面","碗");
        final var flour = COMMODITY("面粉","碗");
        final var house = COMMODITY("房子","棟");
        final var ironstone = COMMODITY("铁矿石","吨");
        final var steel = COMMODITY("钢材","吨");
        final var machinetool = COMMODITY("机床","台");
        
        // 农业经济
        final var industry1 = SimpleMarket.of("第一产业",grain);// 创建一个市场经济
        final var miner = industry1.getCommodityLicense("矿工",labor);// 生产一个矿工
        final var iron_mine = industry1.getCommodityLicense("铁矿",ironstone);// 生产一个铁矿
        final var steel_plant = industry1.getCommodityLicense("炼钢厂",steel);
        final var machinetool_plant = industry1.getCommodityLicense("机床厂",machinetool);
        
        iron_mine.purchase(miner,labor,1000000,rmb,20000 );// 铁矿厂雇佣矿工
        iron_mine.purchase(iron_mine,ironstone,1000000,labor,10);// 矿工挖铁矿
        
        steel_plant.purchase(iron_mine, ironstone,10000000000l,rmb,1000000);// 炼钢厂买入铁矿石
        machinetool_plant.purchase(steel_plant,steel,10000000000l,rmb,1000000);// 
        
        // 农业经济
        final var agriculture = SimpleMarket.of("要素市场",grain);// 创建一个市场经济
        final var wheal_farmland = agriculture.getCommodityLicense("麦田",wheal);// 生产一块麦田
        final var zhangsan_farmer = agriculture.getCommodityLicense("张三",labor);// 生产一个麦农
        zhangsan_farmer.work(wheal_farmland,50000000,100);// 张三在农场辛勤劳动
        
        // 商业经济
        final var business = SimpleMarket.of("贸易市场",rmb);// 
        final var zhangsan_consumer = business.getCommodityLicense("张三");// 
        final var tractor_supplier = business.getCommodityLicense("拖拉机厂",tractor);//
        final var noodle_supplier = business.getCommodityLicense("拉面馆",noodel);// 
        final var house_supplier = business.getCommodityLicense("房地产商",house);// 
        final var flour_supplier = business.getCommodityLicense("面粉厂",flour);// 面粉厂够买面粉
        
        // 拖来机厂技术革新
        tractor_supplier.purchase(machinetool_plant,machinetool,200,rmb,500000); // 拖拉机厂够买机床
        tractor_supplier.purchase(steel_plant,steel,5000,rmb,300000); // 拖拉机厂够买机床
        
        // 面粉厂技术革新
        flour_supplier.purchase(zhangsan_farmer,wheal,500000,rmb,1500000);  // 面粉厂刚从张三家里购入
        
        // 张三消费
        zhangsan_consumer.purchase(tractor_supplier,5000);// 从拖拉机厂买一台拖拉机
        zhangsan_consumer.purchase(noodle_supplier,13);
        zhangsan_consumer.purchase(noodle_supplier,13);
        zhangsan_consumer.purchase(noodle_supplier,13);
        zhangsan_consumer.purchase(noodle_supplier,13);
        zhangsan_consumer.purchase(house_supplier,5000000);// 张三花了5百万买了一栋房子
        
        System.out.println("张三的经营状况:");
        final var licenseGroup = new CommodityLicenseGroup("张三",zhangsan_farmer,zhangsan_consumer);
        licenseGroup.trialBalance((Predicate<IRecord>)null).forEach(System.out::println);
        
        System.out.println("\n食堂的经营状况:");
        final var licenseGroup2 = new CommodityLicenseGroup("拉面馆",noodle_supplier);
        licenseGroup2.trialBalance((Predicate<IRecord>)null).forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\n拖拉机财务状况:");
        final var licenseGroup3 = new CommodityLicenseGroup("拖拉机厂",tractor_supplier);
        licenseGroup3.trialBalance((Predicate<IRecord>)null).forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\n铁矿厂财务状况:");
        final var licenseGroup4 = new CommodityLicenseGroup("铁矿",iron_mine);
        licenseGroup4.trialBalance((Predicate<IRecord>)null).forEach(e->{
            System.out.println(e);
        });
        
        /**
         * 汇总处理
         */
        System.out.println("\n所有的交易情况:");
        final var trans = Stream.of(industry1,agriculture,business).flatMap(mkt->mkt.getTransactions().stream());
        trans.forEach(e->{
            System.out.println(e);
        });
        
    }
}
